import { GlobalFunction } from "./GlobalFunction";
import { GlobalEvent } from "./GlobalEvent";
import { ConstData } from "./ConstData";

const {ccclass, property} = cc._decorator;

@ccclass
export default class panelBase extends cc.Component {

    @property(cc.Node)
    box:cc.Node = null;
    @property(Boolean)
    isMoveable:boolean = false;
    @property(Boolean)
    isMaskBottom:boolean = true;

    private _data:any = null;
    private _panelId:number = null;

    private _isPlayingClose:boolean = false;
    private _parentLayer:cc.Node = new cc.Node();
    private _bottomLayer:cc.Node = new cc.Node();
    private _topLayer:cc.Node = new cc.Node();
    private _Animation:cc.Animation = null;

    private _initLayer(){
        this._parentLayer.name = this.node.name;
        this._parentLayer.group = "UI";
        if(cc.director.getScene().name == "map")
            this._parentLayer.parent = cc.find("Canvas/UI");
        else
            this._parentLayer.parent = cc.find("Canvas");
        
        GlobalFunction.addWidgetCanvas(this._parentLayer);
        this._bottomLayer.parent = this._parentLayer;
        this.node.parent = this._parentLayer;
        this._topLayer.parent = this._parentLayer;
        this._bottomLayer.name = "bottom";
        this.node.name = "content";
        this._topLayer.name = "top";
        this._moveable();
    }

    private _playAnimation(){
        this._Animation = this.node.getComponent(cc.Animation);
        if(this._Animation)
        {
            var _aniClips = this._Animation.getClips();
            var _aniName = _aniClips[0].name
            if(_aniName)
            {
                this._Animation.play(_aniName)
            }
        }
    }

    //底部黑色透明遮挡
    private async _addBottomBlackSplash()
    {
        if(!this.box || !this.isMaskBottom) return;
        var splashSp = await GlobalFunction.asyncLoadPrefabEx("prefab/splashSprite", this);
        var splashSpNode = cc.instantiate(splashSp);
        splashSpNode.active = false;
        GlobalFunction.addWidgetCanvas(splashSpNode);
        splashSpNode.color = new cc.Color(0,0,0);
        splashSpNode.opacity = 120;
        splashSpNode.on(cc.Node.EventType.TOUCH_END,this.close.bind(this), this);
        splashSpNode.active = true;
        splashSpNode.parent = this._bottomLayer;
    }

    //可移动窗口
    private _moveable(){
        if(!this.isMoveable) return;
        if(!this.box) return;

        var startMove = false;
        var startTouchPos = cc.v2(0,0);
        var moveTouchPos = cc.v2(0,0);
        var startNodePos = cc.v2(0,0);
        this.box.on(cc.Node.EventType.TOUCH_START, function(event){
            startMove = true;
            startTouchPos = event.touch.getLocation();
            startNodePos = this.node.position;
        }, this);

        this.box.on(cc.Node.EventType.TOUCH_MOVE, function(event){
            if(startMove)
            {
                moveTouchPos = event.touch.getLocation();
                var nodePos = cc.v2(0, 0);
                nodePos.x = startNodePos.x + moveTouchPos.x - startTouchPos.x;
                nodePos.y = startNodePos.y + moveTouchPos.y - startTouchPos.y;
                this.node.setPosition(nodePos);
            }
        }, this);

        this.box.on(cc.Node.EventType.TOUCH_END, function(event){
            startMove = false;
        }, this)
    }

    async _openPanel(panelId:number, data?:any){
        this._data =  data;
        this._panelId = panelId;
        this.node.active = false;
        this._initLayer();
        try{
            await this._addBottomBlackSplash();
            this._playAnimation();
            this.node.active = true;
            this.open(data);
        }catch(e){
            cc.log(e);
        }
    }

    async close()
    {
        var _aniClips = this._Animation.getClips();
        var _aniName = _aniClips[1].name
        if(!_aniName) return;
        if(this._isPlayingClose) return;
        this._isPlayingClose = true;
        try{
            await GlobalFunction.asyncPlayAnimationEx(this._Animation, _aniName, this)
            GlobalEvent.emit(GlobalEvent.Event.onPanelClose, {PanelId: this._panelId});
            this._parentLayer.destroy();
        }catch(e){
            cc.log(e);
        }
    }

    open(data?:any){
    }
}
